import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-09-11
 * Time:22:05
 */
//异常
class Person{
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Login0 {
    private String userName = "admin";
    private String passWord = "123456";

    /*public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassWord() {
        return passWord;
    }
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }*/

    public void loginInfo(String userName,String passWord) {
        if (!this.userName.equals(userName)) {
            System.out.println("用户名错误！");
            return;
        }
        if (!this.passWord.equals(passWord)) {
            System.out.println("密码错误！");
            return;
        }
        System.out.println("登录成功！");
    }
}

class Login {
    private String userName = "admin";
    private String passWord = "123456";

    public void loginInfo(String userName,String passWord) throws UserNameException,PassWordException{
        if (!this.userName.equals(userName)) {
            throw new UserNameException("用户名错误异常！");
        }
        if (!this.passWord.equals(passWord)) {
            throw new PassWordException("密码错误异常！");
        }

        /*try {
            if (!this.passWord.equals(passWord)) {
                throw new PassWordException("密码错误异常！");
            }
        }catch (PassWordException e) {
            e.printStackTrace();
        }*/

        System.out.println("登录成功！");
    }
}

public class Test {
    public static void main(String[] args){
        Login login = new Login();
        try {
            login.loginInfo("admin","123456");
        }catch (UserNameException e) {
            e.printStackTrace();
        }catch (PassWordException e) {
            e.printStackTrace();
        }finally {

        }
    }
    public static void main16(String[] args) {
        Login login = new Login();
        login.loginInfo("admin","123456");
    }
    public static void func() {
        //本方法func中没有处理异常
        int[] arr = {1, 2, 3};
        System.out.println(arr[100]);
    }

    public static void main15(String[] args) {
        //沿着调用栈向上传递，到main方法中也没有处理异常，交给JVM处理
        func();
        System.out.println("after try catch");
    }
    public static void main14(String[] args) {
        //本方法func中没有合适的处理异常的方式, 就会沿着调用栈向上传递，到main方法中
        try {
            func();
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }
    public static int testFinally() {
        try {
            return 10;
        }catch (InputMismatchException e) {
            e.printStackTrace();
        }finally {
            return 20;
        }
    }
    public static void main13(String[] args) {
        System.out.println(testFinally());
    }
    public static int getDate() {
        try (Scanner scanner = new Scanner(System.in)) {
            int date = scanner.nextInt();
            return date;
        } catch (InputMismatchException e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally中的代码");
        }

        System.out.println("try-catch-finally之后的代码");
        /*if (scanner != null) {
            scanner.close();
        }*/
        return 0;
    }
    public static void main12(String[] args) {
        int date = getDate();
        System.out.println(date);
    }
    public static void main10(String[] args) {
        int[] array = {1,2,3};
        try {
            System.out.println("before");
            array = null;
            System.out.println(array[100]);
            System.out.println("……");
        }catch (Exception e) {
            System.out.println("处理Exception异常");
            e.printStackTrace();
        }
        System.out.println("after");
    }
    public static void main11(String[] args) {
        int[] array = {1,2,3};
        try {
            System.out.println(array[21]);
            array = null;
            System.out.println(array.length);
        }catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("处理ArrayIndexOutOfBoundsException异常");
            e.printStackTrace();
        }catch (NullPointerException e) {
            System.out.println("处理NullPointerException异常");
            e.printStackTrace();
        }finally {
            System.out.println("finally中的代码一定会执行");
        }
        System.out.println("如果没有抛出异常，或者异常被处理了，try-catch后的代码也会执行");
    }
    public static void main9(String[] args) {
        int[] array = {1,2,3};
        try {
            System.out.println(array[21]);
            array = null;
            System.out.println(array.length);
        }catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("处理ArrayIndexOutOfBoundsException异常");
            e.printStackTrace();
        } catch (NullPointerException e) {
            System.out.println("处理NullPointerException异常");
            e.printStackTrace();
        }
        System.out.println("after");
    }
    public static void main8(String[] args) {
        int[] array = {1,2,3};
        try {
            System.out.println(array[4]);//此处会抛出数组越界异常
        }catch (NullPointerException e) { //捕获时候捕获的是空指针异常--真正的异常无法被捕获到
            System.out.println("处理NullPointerException");
            e.printStackTrace();
        }
        System.out.println("after");
    }
    public static void main7(String[] args) {
        Person person = new Person();
        try {
            //尝试调用可能会抛出异常的方法
            Person person1 = (Person) person.clone();
            System.out.println("clone successful!");
            System.out.println(person1);
        } catch (CloneNotSupportedException c) {
            //真正地处理异常：捕获它并做出响应
            System.out.println("捕获了异常");
            c.printStackTrace();//printStackTrace方法可以输出异常信息：打印异常的类型、详细消息以及异常发生的位置
        }
        //处理完异常后，程序可以继续运行
        System.out.println("程序正常，继续执行");
    }
    public static int getElement(int[] array,int index) {
        if (array == null) {
            throw new NullPointerException("传递的数组为null");
        }
        if (index < 0 || index >= array.length) {
            throw new ArrayIndexOutOfBoundsException("传递的数组下标越界");
        }

        return array[index];
    }
    public static void main5(String[] args) {
        //异常的抛出：throw关键字
        //实现一个获取数组中任意位置元素的方法
        int[] array = {1,2,3};
        int ret = getElement(array,3);
        System.out.println(ret);
    }
    public static void main6(String[] args) throws CloneNotSupportedException {
        //编译时异常/受查异常
        Person person = new Person();
        Person person1 = (Person) person.clone();
        System.out.println("抛给JVM处理，程序立即终止");
        System.out.println(person1);
    }
    public static void main3(String[] args) {
        //空指针异常
        int[] array = null;
        System.out.println(array.length);//不能对空指针进行访问
        System.out.println("abcdef");
    }
    public static void main2(String[] args) {
        //数组越界异常
        int[] array = {1,2,3,4};
        System.out.println(array[10]);
        System.out.println("abcdef");//这里也不会执行
    }
    public static void main1(String[] args) {
        //算术异常
        System.out.println(10 / 0);
        System.out.println("abcdef");
    }
}
